home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / stevie.arc / MAIN.C < prev    next >
Text File  |  1990-01-10  |  7KB  |  308 lines

  1. /*
  2.  * STEVIE - ST Editor for VI Enthusiasts   ...Tim Thompson...twitch!tjt...
  3.  *
  4.  * Extensive modifications by:  Tony Andrews       onecom!wldrdg!tony
  5.  * Turbo C 1.5 port by: Denny Muscatelli 061988
  6.  */
  7.  
  8. #include "stevie.h"
  9.  
  10. int Rows;        /* Number of Rows and Columns */
  11. int Columns;        /* in the current window. */
  12.  
  13. char *Realscreen = NULL;    /* What's currently on the screen, a single */
  14.                 /* array of size Rows*Columns. */
  15. char *Nextscreen = NULL;    /* What's to be put on the screen. */
  16.  
  17. char *Filename = NULL;    /* Current file name */
  18.  
  19. LPTR *Filemem;        /* The contents of the file, as a single array. */
  20.  
  21. LPTR *Fileend;        /* Pointer to the end of the file in Filemem. */
  22.             /* (It points to the byte AFTER the last byte.) */
  23.  
  24. LPTR *Topchar;        /* Pointer to the byte in Filemem which is */
  25.             /* in the upper left corner of the screen. */
  26.  
  27. LPTR *Botchar;        /* Pointer to the byte in Filemem which is */
  28.             /* just off the bottom of the screen. */
  29.  
  30. LPTR *Curschar;        /* Pointer to byte in Filemem at which the */
  31.             /* cursor is currently placed. */
  32.  
  33. int Cursrow, Curscol;    /* Current position of cursor */
  34.  
  35. int Cursvcol;        /* Current virtual column, the column number of */
  36.             /* the file's actual line, as opposed to the */
  37.             /* column number we're at on the screen.  This */
  38.             /* makes a difference on lines that span more */
  39.             /* than one screen line. */
  40.  
  41. int Curswant = 0;    /* The column we'd like to be at. This is used */
  42.             /* try to stay in the same column through up/down */
  43.             /* cursor motions. */
  44.  
  45. bool_t set_want_col;    /* If set, then update Curswant the next time */
  46.             /* through cursupdate() to the current virtual */
  47.             /* column. */
  48.  
  49. int State = NORMAL;    /* This is the current state of the command */
  50.             /* interpreter. */
  51.  
  52. int Prenum = 0;        /* The (optional) number before a command. */
  53.  
  54. LPTR *Insstart;        /* This is where the latest insert/append */
  55.             /* mode started. */
  56.  
  57. bool_t Changed = 0;    /* Set to 1 if something in the file has been */
  58.             /* changed and not written out. */
  59.  
  60. bool_t Debug = 0;
  61.  
  62. char Redobuff[1024];    /* Each command should stuff characters into this */
  63.             /* buffer that will re-execute itself. */
  64.  
  65. char Undobuff[1024];    /* Each command should stuff characters into this */
  66.             /* buffer that will undo its effects. */
  67.  
  68. char Insbuff[1024];    /* Each insertion gets stuffed into this buffer. */
  69.  
  70. LPTR *Uncurschar;    /* Curschar is restored to this before undoing. */
  71.  
  72. int Ninsert = 0;    /* Number of characters in the current insertion. */
  73. int Undelchars = 0;    /* Number of characters to delete, when undoing. */
  74. char *Insptr = NULL;
  75.  
  76. char **files;        /* list of input files */
  77. int  numfiles;        /* number of input files */
  78. int  curfile;        /* number of the current file */
  79.  
  80. static void
  81. usage()
  82. {
  83.     fprintf(stderr, "usage: stevie [file ...]\n");
  84.     fprintf(stderr, "       stevie -t tag\n");
  85.     fprintf(stderr, "       stevie +[num] file\n");
  86.     fprintf(stderr, "       stevie +/pat  file\n");
  87.     exit(1);
  88. }
  89.  
  90. main(argc,argv)
  91. int    argc;
  92. char    *argv[];
  93. {
  94.     char    *initstr, *getenv();    /* init string from the environment */
  95.     char    *tag = NULL;        /* tag from command line */
  96.     char    *pat = NULL;        /* pattern from command line */
  97.     int    line = -1;        /* line number from command line */
  98.  
  99.     /*
  100.      * Process the command line arguments.
  101.      */
  102.     if (argc > 1) {
  103.         switch (argv[1][0]) {
  104.         
  105.         case '-':            /* -t tag */
  106.             if (argv[1][1] != 't')
  107.                 usage();
  108.  
  109.             if (argv[2] == NULL)
  110.                 usage();
  111.  
  112.             Filename = NULL;
  113.             tag = argv[2];
  114.             numfiles = 1;
  115.             break;
  116.  
  117.         case '+':            /* +n or +/pat */
  118.             if (argv[1][1] == '/') {
  119.                 if (argv[2] == NULL)
  120.                     usage();
  121.                 Filename = strsave(argv[2]);
  122.                 pat = &(argv[1][1]);
  123.                 numfiles = 1;
  124.  
  125.             } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  126.                 if (argv[2] == NULL)
  127.                     usage();
  128.                 Filename = strsave(argv[2]);
  129.                 numfiles = 1;
  130.  
  131.                 line = (isdigit(argv[1][1])) ?
  132.                     atoi(&(argv[1][1])) : 0;
  133.             } else
  134.                 usage();
  135.  
  136.             break;
  137.  
  138.         default:            /* must be a file name */
  139.             Filename = strsave(argv[1]);
  140.             files = &(argv[1]);
  141.             numfiles = argc - 1;
  142.             break;
  143.         }
  144.     } else {
  145.         Filename = NULL;
  146.         numfiles = 1;
  147.     }
  148.     curfile = 0;
  149.  
  150.     windinit();
  151.  
  152.     /*
  153.      * Allocate LPTR structures for all the various position pointers
  154.      */
  155.     if ((Filemem = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  156.         fprintf(stderr, "Can't allocate data structures\n");
  157.         windexit(0);
  158.     }
  159.     if ((Fileend = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  160.         fprintf(stderr, "Can't allocate data structures\n");
  161.         windexit(0);
  162.     }
  163.     if ((Topchar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  164.         fprintf(stderr, "Can't allocate data structures\n");
  165.         windexit(0);
  166.     }
  167.     if ((Botchar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  168.         fprintf(stderr, "Can't allocate data structures\n");
  169.         windexit(0);
  170.     }
  171.     if ((Curschar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  172.         fprintf(stderr, "Can't allocate data structures\n");
  173.         windexit(0);
  174.     }
  175.     if ((Insstart = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  176.         fprintf(stderr, "Can't allocate data structures\n");
  177.         windexit(0);
  178.     }
  179.     if ((Uncurschar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  180.         fprintf(stderr, "Can't allocate data structures\n");
  181.         windexit(0);
  182.     }
  183.  
  184.     screenalloc();
  185.     filealloc();        /* Initialize Filemem & Fileend */
  186.  
  187.     screenclear();
  188.  
  189.     if ((initstr = getenv("EXINIT")) != NULL) {
  190.         char *lp, buf[128];
  191.  
  192.         if ((lp = getenv("LINES")) != NULL) {
  193.             sprintf(buf, "%s lines=%s", initstr, lp);
  194.             readcmdline(':', buf);
  195.         } else
  196.             readcmdline(':', initstr);
  197.     }
  198.  
  199.     if (Filename != NULL) {
  200.         if (readfile(Filename, Filemem, FALSE))
  201.             filemess("[New File]");
  202.     } else
  203.         msg("Empty Buffer");
  204.  
  205.     setpcmark();
  206.  
  207.     updatescreen();
  208.     
  209.     if (tag) {
  210.         stuffin(":ta ");
  211.         stuffin(tag);
  212.         stuffin("\n");
  213.  
  214.     } else if (pat) {
  215.         stuffin(pat);
  216.         stuffin("\n");
  217.  
  218.     } else if (line >= 0) {
  219.         if (line > 0)
  220.             stuffnum(line);
  221.         stuffin("G");
  222.     }
  223.  
  224.     edit();
  225.  
  226.     windexit(0);
  227.         return(0);
  228. }
  229.  
  230. #define    RBSIZE    1280        /* should be a little bigger than YBSIZE */
  231. static char getcbuff[RBSIZE];
  232. static char *getcnext = NULL;
  233.  
  234. void
  235. stuffin(s)
  236. char *s;
  237. {
  238.     if ( getcnext == NULL ) {
  239.         strcpy(getcbuff,s);
  240.         getcnext = getcbuff;
  241.     } else
  242.         strcat(getcbuff,s);
  243. }
  244.  
  245. void
  246. stuffnum(n)
  247. int    n;
  248. {
  249.     char    buf[32];
  250.  
  251.     sprintf(buf, "%d", n);
  252.     stuffin(buf);
  253. }
  254.  
  255. void
  256. addtobuff(s,c1,c2,c3,c4,c5,c6)
  257. char *s;
  258. char c1, c2, c3, c4, c5, c6;
  259. {
  260.     char *p = s;
  261.     if ( (*p++ = c1) == NUL )
  262.         return;
  263.     if ( (*p++ = c2) == NUL )
  264.         return;
  265.     if ( (*p++ = c3) == NUL )
  266.         return;
  267.     if ( (*p++ = c4) == NUL )
  268.         return;
  269.     if ( (*p++ = c5) == NUL )
  270.         return;
  271.     if ( (*p++ = c6) == NUL )
  272.         return;
  273. }
  274.  
  275. int
  276. vgetc()
  277. {
  278.     if ( getcnext != NULL ) {
  279.         int nextc = *getcnext++;
  280.         if ( *getcnext == NUL ) {
  281.             *getcbuff = NUL;
  282.             getcnext = NULL;
  283.         }
  284.         return(nextc);
  285.     }
  286.     return(inchar());
  287. }
  288.  
  289. int
  290. vpeekc()
  291. {
  292.     if ( getcnext != NULL )
  293.         return(*getcnext);
  294.     return(-1);
  295. }
  296.  
  297. /*
  298.  * anyinput
  299.  *
  300.  * Return non-zero if input is pending.
  301.  */
  302.  
  303. bool_t
  304. anyinput()
  305. {
  306.     return (getcnext != NULL);
  307. }
  308.